{block name="main"}
<table class="table table-bordered text-center mini-program-manage">
    <tbody>
    <tr>
        <td>
            <div class="box">
                <div class="tag"><i>1</i></div>
                <div class="qr">
                    <div><img src="{$testQrCodeUrl}"></div>
                </div>
                <p>绑定体验者后，随时可以通过帐号查看最新效果</p>
                <div>
                    <button class="btn btn-primary" id="bind_tester">绑定体验者</button>
                </div>
            </div>
        </td>
        <td>
            <div class="box">
                <div class="tag"><i>2</i></div>
                <div class="qr">
                    <div><i class="icon icon-refresh"></i></div>
                </div>
                <p>发布将在<span class="text-bright">1~3个工作日</span>完成，发布状态自动下发微信通知</p>
                <div>
                        <button class="btn btn-success" id="public" disabled="disabled" value="{$status}" >提交发布</button>
                </div>
            </div>
        </td>
        <td>
            <div class="box">
                <div class="tag"><i>3</i></div>
                <div class="qr">
                    <div><img src="" id="sunCode"></div>
                </div>
                <p>发布成功后下载太阳码<span class="text-bright">打印或转发</span>做推广</p>
                <div>
                    <a href="{$downSunCodeUrl}?auth_id={$mp_info.auth_id}" class="btn btn-primary">下载太阳码</a>
                </div>
            </div>
        </td>
    </tr>
    </tbody>
</table>
<input type="hidden" id="hidden_auth_id" value="{$mp_info.auth_id}">
<table class="table v-table table-auto-center">
    <thead>
    <tr>
        <th>发布人</th>
        <th>发布状态</th>
        <th>发布时间</th>
        <th>备注</th>
    </tr>
    </thead>
    <tbody id="submit_list">
    </tbody>
</table>
<div id="tips1" style="display: none;">
    <div class="mb-10">当前小程序没有“服务类目”，请先前往微信商户平台添加类目再重新提交发布。</div>
    <div class="pull-right">
        <a href="https://mp.weixin.qq.com/" target="_blank" class="btn btn-success">前往公众平台</a>
        <a href="javascript:void(0);" class="btn btn-primary" id = "re-public">重新发布</a>
    </div>
</div>
<input type="hidden" id="page_index">
<nav aria-label="Page navigation" class="clearfix">
    <ul id="page" class="pagination pull-right"></ul>
</nav>
<div id="saveTips" style="display: none">
    <div class="saving"></div>
    <div class="saveing-box">
        <div><img src="/public/platform/images/loading.svg" alt="" style="width: 80px;height: 80px"></div>
        <p>提交发布中</p>
    </div>
</div>
{/block}
{block name="script"}
<script id="tpl_submit_list" type="text/html">
    <%each data as item item_id%>
    <tr>
        <td>
            <%item.user%>
        </td>
        <td>
            <%if item.status == 0%>
            <span class="text-success">审核通过</span>
            <%else if item.status == 1%>
            <span class="text-danger">审核失败</span>
            <%else if item.status == 2%>
            <span>审核中</span>
            <%else if item.status == 3%>
            <span class="text-danger">发布失败</span>
            <%else if item.status == 4%>
            <span class="text-success">发布成功</span>
            <%else%>
            <%/if%>
        </td>
        <td>
            <%item.submit_date%>
        </td>
        <td>
            <div class="max-w-auto margin-auto-temp text-left">
                <%item.review_message%>
            </div>
        </td>
    </tr>
    <%/each%>
</script>
<script>
    $(document).ready(function (){
        var public_style = $("#public").val();
        if (public_style == 2) {
            $("#public").attr("disabled", true);
            $("#public").text("审核中");
        }
        var check_satatus = setInterval(function(){
            $.ajax({
                type: "GET",
                url: "{$getPublicStatusUrl}",
                success: function (data) {
                    if (data == 2) {
                        $("#public").attr("disabled", true);
                        $("#public").text("审核中");
                    } else if (data == 4) {
                        clearInterval(check_satatus);
                        $("#public").attr("disabled", false);
                        $("#public").text("提交发布");
                    } else {
                        $("#public").attr("disabled", false);
                        $("#public").text("提交发布");
                    }
                }
            })
        },3000)
    })
</script>
<script>
    require(["util", "tpl"], function (util, tpl) {
        // 太阳码
        $.ajax({
            type: "get",
            url: "{$sunCodeUrl}",
            success: function (data) {
                $("#sunCode").attr("src", __IMG(data.data));
            }
        });

        util.initPage(LoadingInfo);
        $("#bind_tester").on('click', function () {
            var html = '<form class="form-horizontal padding-15" id="">';
            html += '<div class="form-group"><label class="col-md-3 control-label">微信号<span class="red">*</span></label><div class="col-md-8"><input class="form-control" id="wchat_id"></div></div>';
            html += '</form>';
            var auth_id = $("#hidden_auth_id").val();
            util.confirm('绑定体验者', html, function () {
                //执行确认后的逻辑
                var wchat_id = $("#wchat_id").val();
                if (wchat_id == '') {
                    util.message('微信号不能为空')
                    return false;
                }
                $.ajax({
                    type: "post",
                    url: "{$bindMpTesterUrl}",
                    data: {
                        'auth_id': auth_id,
                        'wchat_id': wchat_id
                    },
                    success: function (data) {
                        if (data["code"] > 0) {
                            util.message(data["message"], 'success');
                        } else {
                            util.message(data["message"]);
                        }
                    }
                });
            })
        })

        $("#public").on('click', function () {
            $.ajax({
                type: "get",
                url: "{$isUseMiniProgramUrl}",
                success: function (data) {
                    if (data.code < 0) {
                        var result = data.data;
                        if (result.is_shop_open == 0) {
                            util.alert("请前往小程序“基本设置”打开小程序商城，再发布", function () {
                                window.location.href = __URL('ADDONS_MAINminiProgramSetting');
                            });
                        } else if (result.is_has_category == 0) {
                            var html = $("#tips1").html();
                            util.confirm2("提示", html, 'medium');
                        } else {
                            util.alert('确认发布吗？', function(){
                                commitMp();
                            })
                        }
                    }
                }
            });
        })

        // 发布
        function commitMp() {
            $('#saveTips').show();
            $.ajax({
                type: "post",
                url: "{$commitUrl}",
                success: function (data) {
                    $('#saveTips').hide();
                    if (data["code"] > 0) {
                        util.message(data["message"], 'success');
                        util.initPage(LoadingInfo);
                    } else {
                        util.message(data["message"], 'danger');
                    }
                }
            });

            // 定时
            $.ajax({
                type: "GET",
                url: "{$getPublicStatusUrl}",
                success: function (data) {
                    if (data == 2) {
                        $("#public").attr("disabled", true);
                        $("#public").text("审核中");
                    } else {
                        $("#public").attr("disable", false);
                        $("#public").text("提交发布");
                    }
                }
            })
        }
        // 重新发布
        $("body").on('click','#re-public',function (){
            $(".jconfirm").remove();
            commitMp()
        })

        $(".J-submit").on('click', function () {
            util.confirm('体验者列表', 'url:{$submitModalUrl}', function () {
                var address = this.$content.find('select[name="address[]"]').val();
                var title = this.$content.find('input[name="title[]"]').val();
                var first_id = this.$content.find('select[name="first_class[]"]').val();
                var first_class = this.$content.find('select[name="first_class[]"]:selected').text();
                var second_id = this.$content.find('select[name="second_class[]"]').val();
                var second_class = this.$content.find('select[name="second_class[]"]:selected').text();
                var third_id = this.$content.find('select[name="third_class[]"]').val();
                var third_class = this.$content.find('select[name="third_class[]"]:selected').text();
                var tag = this.$content.find('input[name="tag[]"]').val();
                $.ajax({
                    type: 'POST',
                    url: '{$submitUrl}',
                    data: {
                        address: address,
                        title: title,
                        first_id: first_id,
                        first_class: first_class,
                        second_id: second_id,
                        second_class: second_class,
                        third_id: third_id,
                        third_class: third_class,
                        tag: tag
                    },
                    success: function (data) {
                        if (data.code > 0) {
                            util.message('提交成功', 'success');
                        } else {
                            util.message(data.message, 'success');
                        }
                    }
                })
            }, 'large')
        })

        function LoadingInfo(page_index) {
            $("#page_index").val(page_index);
            $.ajax({
                type: "get",
                url: "{$submitListUrl}",
                success: function (data) {
                    var html = '<tr><td class="h-200" colspan="4">暂无符合条件的数据记录</td></tr>';
                    if (data.data) {
                        if (tpl('tpl_submit_list', data)) {
                            $("#submit_list").html(tpl('tpl_submit_list', data));
                        } else {
                            $("#submit_list").html(html);
                        }
                    } else {
                        $("#submit_list").html(html);
                    }
                }
            });
        }
    })
</script>
{/block}
